package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.callshow.CallShowEntry;
import com.stark.downloader.Downloader;
import com.stark.downloader.FailCause;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.WallpaperDetailAdapter;
import flc.ast.databinding.ActivityAvatarDetailBinding;
import stark.common.bean.StkResBean;

public class AvatarDetailActivity extends BaseAc<ActivityAvatarDetailBinding> {

    public static List<StkResBean> listAvatar;
    public static int selPosition;
    private WallpaperDetailAdapter avatarAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_avatar_detail;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAvatarDetailBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivApply.setOnClickListener(this);
        mDataBinding.ivSave.setOnClickListener(this);
        mDataBinding.avatarBanner.isAutoLoop(false);
        avatarAdapter = new WallpaperDetailAdapter(listAvatar);
        mDataBinding.avatarBanner.setAdapter(avatarAdapter, false);//false不无限循环
        mDataBinding.avatarBanner.setCurrentItem(selPosition, false);
        mDataBinding.avatarBanner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivApply:
                Downloader.newTask(mContext)
                        .url(listAvatar.get(selPosition).getUrl())
                        .saveToPublic(true)
                        //Android10及以上，保存到外部公共存储空间需提供文件名称
                        .fileName(System.currentTimeMillis() + ".jpg")
                        .start(new Downloader.ICallback() {
                            @Override
                            public void onStart(int downloadTaskId) {

                            }

                            @Override
                            public void onProgress(long currentOffset, long totalLength, int percent) {

                            }

                            @Override
                            public void onComplete(@NonNull Uri uri) {
                                CallShowEntry.setWallpaper(uri, new CallShowEntry.SetWallpaperCallback() {
                                    @Override
                                    public void onStart() {
                                        showDialog(getString(R.string.setting_wallpaper));
                                    }

                                    @Override
                                    public void onSetResult(boolean success) {
                                        dismissDialog();
                                        if (success) {
                                            ToastUtils.showShort(getString(R.string.setting_suc));
                                        } else {
                                            ToastUtils.showShort(R.string.setting_failure);
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onFail(FailCause cause) {
                                ToastUtils.showShort(R.string.setting_failure);
                            }
                        });
                break;
            case R.id.ivSave:
                showDialog(getString(R.string.save_ing));
                Downloader.newTask(mContext)
                        .url(listAvatar.get(selPosition).getUrl())
                        .saveToPublic(true)
                        //Android10及以上，保存到外部公共存储空间需提供文件名称
                        .fileName(System.currentTimeMillis() + ".jpg")
                        .start(new Downloader.ICallback() {
                            @Override
                            public void onStart(int downloadTaskId) {

                            }

                            @Override
                            public void onProgress(long currentOffset, long totalLength, int percent) {

                            }

                            @Override
                            public void onComplete(@NonNull Uri uri) {
                                dismissDialog();
                                ToastUtils.showShort(getString(R.string.save_to_album));
                            }

                            @Override
                            public void onFail(FailCause cause) {
                                dismissDialog();
                                ToastUtils.showShort(R.string.save_failure);
                            }
                        });
                break;
        }
    }

    @Override
    protected void initData() {

    }
}